<?php
/*
 * @Author: liuyang
 * @Description:  
 * @Date: 2019-07-15 17:17:30
 * @LastEditors: liuyang
 * @LastEditTime: 2019-07-27 22:22:43
 * @Version: 1.0
 */

namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Exception;
use app\common\model\BaseModel;
use app\common\model\RoleModel;
use function GuzzleHttp\json_encode;

class RoleController extends Controller
{

    public function index()
    {
        return view();
    }

    public function page()
    {
        if (request()->isAjax()) {
            $post = $this->request->post();
            $role = new RoleModel();
            $result = $role->page(($post['page'] - 1) * $post['limit'], $post['limit'], '', '');
            return pagejson($result['data'], $result['count']);
        }
    }


    public function add()
    {
        if (!request()->isAjax()) {
            $data['role'] = null;
            $data['roleList'] = RoleModel::all();
            $data['rolePermissions'] = json_encode([]);
            return view('form', $data);
        }
        $post = request()->post();
        $role = new RoleModel();
        $role->allowField(true)->save(request()->post());
        $result = $role->permissions()->saveAll($post['permissions']);
        if ($result > 0) {
            return zbcjson(true);
        } else {
            return zbcjson(false, '插入错误');
        }
    }


    public function edit($id = 0)
    {
        if (!$this->request->isAjax()) {
            $role = RoleModel::get($id);
            $parentRole = RoleModel::where('id', $role['pid'])->find();
            //下啦列表
            $data['roleList'] = RoleModel::all();
            //当前编辑角色信息
            $data['role'] = $role->toArray();
            //当前编辑角色的关联权限
            $data['rolePermissions'] = json_encode($role->permissions->where('level', '3')->column('id'));
            //当前角色所属角色
            $data['parentRole'] = $parentRole->toArray();
            //所属角色的权限信息
            $data['permissions'] = $parentRole->permissions->toArray();
            return view('form', $data);
        }
        $post = $this->request->post();
        $role = new RoleModel();
        $result = $role->allowField(true)->save($post, ['id' => $post['id']]);
        //删除中间表原始数据
        $role->permissions()->detach();
        //增加中间表的数据
        $role->permissions()->saveAll($post['permissionsIds']);
        if ($result) {
            return zbcjson(true);
        }
        return zbcjson(false, '修改错误');
    }


    public function delete($id = null)
    {
        //删除角色 要同时删除 角色权限关系表的数据 还要检查一下有没有用户此时正在关联这个角色
        $post = $this->request->post();
        $result  = true;

        if (!empty($post['list'])) {
            //批量删除不能保证 事务
            foreach ($post['list'] as $val) {
                $item = RoleModel::get($val);
                $item->startTrans();
                $r1 = $item->permissions()->detach();
                $r2 = $item->delete();
                if ($r1 && $r2) {
                    $item->commit();
                } else {
                    $result = false;
                    $item->rollback();
                }
            }
        }

        if (!empty($id)) {
            $item = RoleModel::get($id);
            $item->startTrans();
            $r1 = $item->permissions()->detach();
            $r2 = $item->delete();
            if ($r1 && $r2) {
                $item->commit();
            } else {
                $result = false;
                $item->rollback();
            }
        }

        if ($result) {
            return zbcjson(true);
        }

        return zbcjson(false, '删除错误');
    }
}
